﻿using BussinessEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;
using Db4objects.Db4o.Linq;
using Db4objects.Db4o;

namespace DataAccessLayer
{
    public class ImportDA
    {
        public ActionResult<Import> Insert(Import entity)
        {
            ImportDetailDA adapter = new ImportDetailDA();
            ActionResult<Import> rs = new ActionResult<Import>();
            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from Import p in conn
                                where p.ImportID == entity.ImportID
                                select p).FirstOrDefault<Import>();
                    if (temp == null)
                    {
                        var tp = entity.ListDetails;
                        entity.ListDetails = null;
                        conn.Store(entity);
                        foreach (var l in tp)
                            l.Import = GetSingle(entity.ImportID, conn).Object;
                        adapter.Insert(tp, conn);
                        rs.Object = entity;
                    }
                    else
                        rs.AddException(new Exception("Trùng khóa chính"));
                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;

            }
        }

        public ActionResult<Import> Update(Import entity)
        {
            ActionResult<Import> rs = new ActionResult<Import>();
            ImportDetailDA adapter = new ImportDetailDA();
            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from Import p in conn
                                where p.ImportID == entity.ImportID
                                select p).FirstOrDefault<Import>();
                    if (temp != null)
                    {
                        temp.ImportDate = entity.ImportDate;
                        temp.SupplierName = entity.SupplierName;
                        temp.SupplierAddress = entity.SupplierAddress;
                        adapter.Delete(entity.ImportID, conn);
                        adapter.Insert(entity.ListDetails, conn);
                        conn.Store(temp);
                        rs.Object = temp;
                    }
                    else
                        rs.AddException(new Exception("Mã dịch vụ : " + entity.ImportID + " không tồn tại."));
                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<Import> Delete(string MaPN)
        {
            ActionResult<Import> rs = new ActionResult<Import>();
            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from Import p in conn
                                where p.ImportID == MaPN
                                select p).ToList<Import>();
                    if (temp != null)
                    {
                        ImportDetailDA adapter = new ImportDetailDA();
                        adapter.Delete(MaPN, conn);
                        foreach (var i in temp)
                            conn.Delete(i);
                    }
                    else
                        rs.AddException(new Exception("Mã dịch vụ : " + MaPN + " không tồn tại."));
                }
                catch (Exception exx)
                {
                    rs.AddException(exx);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<Import> GetAll()
        {
            ActionResult<Import> rs = new ActionResult<Import>();
            using (var conn = DbConnector.OpenDB())
            {

                try
                {
                    rs.Array = (from Import p in conn
                                select p).ToList<Import>();
                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<Import> GetSingle(string MaPN)
        {
            ActionResult<Import> rs = new ActionResult<Import>();
            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from Import p in conn
                                where p.ImportID == MaPN
                                select p).FirstOrDefault<Import>();
                    if (temp != null)
                    {
                        temp.ListDetails = (from ImportDetail p in conn
                                       where p.ImportID == MaPN
                                       select p).ToList<ImportDetail>();
                        foreach (var i in temp.ListDetails)
                        {
                            i.Service = (from Service p in conn
                                        where p.ServiceID == i.ServiceID
                                        select p).First<Service>();
                            i.Import = (from Import p in conn
                                           where p.ImportID == i.ImportID
                                           select p).FirstOrDefault<Import>();
                        }
                        rs.Object = temp;
                    }
                    else
                        rs.AddException(new Exception("Mã phiếu nhập : " + MaPN + " không tồn tại."));
                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<Import> GetSingle(string MaPN, IObjectContainer conn)
        {
            ActionResult<Import> rs = new ActionResult<Import>();
            try
            {
                var temp = (from Import p in conn
                            where p.ImportID == MaPN
                            select p).FirstOrDefault<Import>();
                if (temp != null)
                {
                    temp.ListDetails = (from ImportDetail p in conn
                                   where p.ImportID == MaPN
                                   select p).ToList<ImportDetail>();
                    foreach (var i in temp.ListDetails)
                    {
                        i.Service = (from Service p in conn
                                    where p.ServiceID == i.ServiceID
                                    select p).FirstOrDefault<Service>();
                        i.Import = (from Import p in conn
                                       where p.ImportID == i.ImportID
                                       select p).FirstOrDefault<Import>();
                    }
                    rs.Object = temp;
                }
                else
                    rs.AddException(new Exception("Mã phiếu nhập : " + MaPN + " không tồn tại."));
            }
            catch (Exception ex)
            {
                rs.AddException(ex);
            }
            return rs;
        }

        public string NextID()
        {
            string nextid = "";
            using (var conn = DbConnector.OpenDB())
            {

                try
                {
                    var temp = (from Import p in conn
                                orderby p.ImportID descending
                                select p).FirstOrDefault<Import>();
                    if (temp == null)
                        return "PN00001";
                    
                    int ma = 0;
                    try
                    {
                        ma = DataUtil.ToInt(temp.ImportID.Substring(2)) + 1;
                    }
                    catch (Exception)
                    {
                        ma = 1;
                    }

                    nextid = "PN";
                    for (int i = 0; i < 5 - ma.ToString().Length; i++)
                        nextid += "0";
                    nextid += ma.ToString();

                    while (true)
                    {
                        temp = (from Import p in conn
                                where p.ImportID == nextid
                                select p).FirstOrDefault<Import>();
                        if (temp == null)
                            break;
                        ma++;
                        nextid = "PN";
                        for (int i = 0; i < 5 - ma.ToString().Length; i++)
                            nextid += "0";
                        nextid += ma.ToString();
                    }
                }
                catch (Exception)
                {
                }
                finally
                {
                    conn.Close();
                }
            }
            return nextid;
        }
    }
}
